#include <stdio.h>
#include <string.h>

void swap_int(int *a, int *b)
{
    int tmp = *a;
    *a = *b;
    *b = tmp;
}

void swap_double(double *a, double *b)
{
    double tmp = *a;
    *a = *b;
    *b = tmp;
}

#define generic_swap(a, b) _Generic((a), \
    int *: swap_int,                     \
    double *: swap_double,               \
    default: printf("Unsupported type.\n")) \
    ((a), (b))

int main()
{
    int int_a = 10, int_b = 20;
    double double_a = 3.14, double_b = 2.71;

    // 使用泛型选择表达式交换整数
    printf("Before swapping integers: %d, %d\n", int_a, int_b);
    generic_swap(&int_a, &int_b);
    printf("After swapping integers: %d, %d\n", int_a, int_b);

    // 使用泛型选择表达式交换浮点数
    printf("Before swapping doubles: %f, %f\n", double_a, double_b);
    generic_swap(&double_a, &double_b);
    printf("After swapping doubles: %f, %f\n", double_a, double_b);

    // char char_a = 'a', char_b = 'b';
    // generic_swap(&char_a, &char_b);         // 编译错误: default: printf("Unsupported type.\n"))((a), (b))
    // printf("After swapping char: %c, %c\n", char_a, char_b);

    return 0;
}